#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>

#define BUFFER_SIZE 1024

int main() {
	    int pipefd[2];
	        pid_t pid;
		    char buffer[BUFFER_SIZE];
		        

		        if (pipe(pipefd) == -1) {
				        perror("pipe");
					        exit(EXIT_FAILURE);
						    }
			    
			    pid = fork();
			        if (pid == -1) {
					        perror("fork");
						        exit(EXIT_FAILURE);
							    }
				    
				    if (pid > 0) { // 父进程
					            close(pipefd[0]); // 关闭读端
						            
						            char message[] = "20242910-林天华"; // 替换为你的学号和姓名
							            printf("父进程写入: %s\n", message);
								            write(pipefd[1], message, strlen(message) + 1);
									            
									            close(pipefd[1]); // 关闭写端
										            wait(NULL); // 等待子进程结束
											        } else { // 子进程
													        close(pipefd[1]); // 关闭写端
														        
														        read(pipefd[0], buffer, BUFFER_SIZE);
															        printf("子进程读取: %s\n", buffer);
																        
																        close(pipefd[0]); // 关闭读端
																	        exit(EXIT_SUCCESS);
																		    }
				        
				        return 0;
}
